/**
 * @author zhaozhenjiang
 * shell脚本执行类
 * */

package com.auto.oracle;

import java.util.LinkedList;

final class shellExecuter implements IStreamListener {
	private LinkedList<String> queue = null;
	private Process proc = null;
	private Runtime rt = null;
	private streamBuffer input = null,
			             error = null;
	
	public shellExecuter(){
		queue = new LinkedList<String>();
		rt = Runtime.getRuntime();
		input = new streamBuffer();
		input.setListener(this);
		input.start();
		error = new streamBuffer();
		error.setListener(this);
		error.start();
	}
	
	public void pushCommand(String cmd){
		queue.addLast(cmd);
	}
	
	public void run(){
		String cmd = null;
		while(true){			
			if(queue.size() == 0)
				break;
			cmd = queue.removeFirst();
			try{
				proc = rt.exec(cmd);
				input.setStream(proc.getInputStream());
				input.relieveWait();
				error.setStream(proc.getErrorStream());
				error.relieveWait();
				proc.waitFor();
				proc.destroy();
			}catch(Exception ex){}
		}
	}

	@Override
	public void filterStream(String out) {
		if(out.indexOf("Successfully Setup Software") >= 0){
			proc.destroy();
		}
		System.out.print(out);
	}
	

	@SuppressWarnings("unused")
	private class Decompile{
		
	}
}
